/**
 * pcm_window 
 * Copyright (c) 2012-2015 Pcmsoft, Inc.
 * @Auth:qiyh
 */

function getOpener(){
	var callerWindowObj = window.dialogArguments;
	if(callerWindowObj != undefined){
		opener = callerWindowObj;
	}
	return opener;
}

function openWindow(sURL, iWidth, iHeight) { 
	iWidth = iWidth?iWidth:"650";
	iHeight = iHeight?iHeight:"450";
	var iLeft = (window.screen.availWidth-10-iWidth)/2; //获得窗口的水平位置;
	var iTop = (window.screen.availHeight-30-iHeight)/2; //获得窗口的垂直位置;
	var prop="width="+iWidth+",height="+iHeight+",scrollbars=yes, status=yes, resizable=yes, top="+iTop+",left="+iLeft;
	window.open(sURL,"myWindow"+Math.ceil(Math.random()*10),prop).focus();
}

// 弹出新窗口
function openNewWindow(url, info){
	if(info==undefined || info==null || info=='')
		info = "toolbar=no, menubar=yes, scrollbars=yes,resizable=yes,location=no, status=yes, center=yes, width=1100";
	window.open(url, "newWindow"+Math.ceil(Math.random()*10), info).focus();
}

// 单例窗口
var nwwindow = null;
function openOneWindow(url, info){
	newwindowClose();
	if(info==undefined || info==null || info=='')
		info = "toolbar=no, menubar=yes, scrollbars=yes,resizable=yes,location=no, status=yes,top=200,left=200,width=950px,height=700px";
	nwwindow = window.open(url, "oneWindow", info);
	nwwindow.focus();
}

// 弹出模态窗口
function openModalWindow(sURL, sWidth, sHeight) {
	sWidth = sWidth?sWidth:"800px";
	sHeight = sHeight?sHeight:"500px";
	if(sURL.indexOf('?')<0)sURL+="?";
	else sURL+="&";
	sURL += "rd="+new Date().getTime();
	window.showModalDialog(sURL,window,"dialogWidth:"+sWidth+"; dialogHeight:"+sHeight+"; edge:sunken; center:Yes; help:No; resizable:Yes; status:No;");
}

function newwindowClose() {
	try {
		if (nwwindow != null) {
			if (!nwwindow.closed) {
				nwwindow.close();
			}
		}
	}catch (e) {
		try {
			nwwindow.close();
			nwwindow = null;
		}catch (e) {}
	}
}

/**
 * 本Js代码用于创建一个自定义的确认窗口,
 * 具体功能包括:自定义窗口标题,自定义窗口内容,是否显示取消按钮,焦点位置设定
 * Author:Qiyh
 * @return
 */

/**
 * 1. 显示弹出消息框
 * @param content
 * @return
 */
function showWait(contStr){
	var sWidth = 350;
	var sHeight = 90;
	create_mask();
	var msgbox=document.getElementById("msgbox");
	if(null != msgbox)
		document.body.removeChild(msgbox);
	var bordercolor="#336699";//提示窗口的边框颜色
	var box=document.createElement("div");
	box.id="msgbox";
	box.style.cssText="width:"+sWidth+";height:"+sHeight+";border:1px solid "+bordercolor+";background:#FFFFFF;position:absolute;overflow:auto;padding:0;z-index:1002;";
	var headTb='<table align="center" border="0" cellspacing="0" width="100%" style="background:'+bordercolor+';font:12px;color:white;filter:progid:DXImageTransform.Microsoft.Alpha(startX=100,startY=100,finishX=20,finishY=20,style=1,opacity=75,finishOpacity=100);opacity:0.75;">';
		headTb += '<tr"><td id="boxTitle" align="left" style="margin:0;padding:2px;">系统消息</td>';
		headTb += '<td align="right" style="margin:0;padding:2px;"><img src="img/window/winClose.png" onclick="javascript:removeDiv();"></td>';
		headTb += '</tr></table>';
	var bodyTb='<table align="center" border="0" cellspacing="0" width="100%" style="background:#FFFFFF;font:12px">';
		bodyTb += '<tr><td id="boxBody" colspan="2" style="padding:2px 0px"><center style="margin-top:22px"><img src="img/loading.gif" style="cursor:default;vertical-align:middle;"/>&nbsp;&nbsp;'+contStr+'</center></td></tr></table>';
	box.innerHTML = headTb+bodyTb;
	document.body.appendChild(box);
	re_pos();
	var maskIframe=document.getElementById("maskIframe");
	if(null==maskIframe || maskIframe==undefined)
		displaySelectTag("hidden");
}

/**
 * 2. 显示带遮罩的窗口:
 * contStr  :弹出对话框的内容,可以使用HTML代码,例如<font color='red'>删除么?</font>,如果直接带入函数,注意转义
 * @param sWidth
 * @param sHeight
 * @return
 */
function showDiv(contStr,sWidth,sHeight){
	create_mask();
	sWidth = sWidth?sWidth:"350";
	sHeight = sHeight?sHeight:"110";
	createMsgBox(contStr,sWidth,sHeight,2);
}

/**
 * 3. 显示图片
 * @param imgUrl
 * @return
 */
function showImg(imgUrl){
	create_mask();
	createMsgBox("<center><img src='"+imgUrl+"'></center>",640,450,0);
}



function get_width(){
	return (document.body.clientWidth+document.body.scrollLeft);
}
function get_height(){
	return (document.body.clientHeight+document.body.scrollTop);
}
function get_left(w){
	var bw=document.body.clientWidth;
	var bh=document.body.clientHeight;
	w=parseFloat(w);
	return (bw/2-w/2+document.body.scrollLeft);
}
function get_top(h){
	var bw=document.body.clientWidth;
	var bh=document.body.clientHeight;
	h=parseFloat(h);
	return (bh/2-h/2+document.body.scrollTop);
}
function get_btn(btnCount){
	var btnStr = "";
	btnCount = btnCount!=undefined?btnCount:0;
	if(0 < btnCount){
		btnStr += "<input type='button' value='确定' style='width:60px;background:#3584D8;color:#fff;cursor:pointer;line-height:20px;border:none;' onclick='javascript:btnOk();'>";
	}
	if(1 < btnCount){
		btnStr += "<input type='button' value='取消' style='width:60px;background:#3584D8;color:#fff;cursor:pointer;line-height:20px;border:none;margin-left:20px;' onclick='javascript:removeDiv();'>";
	}
	if(btnStr!=""){
		btnStr = "<div style='text-align:center;background:#EDF4FA'>"+btnStr+"</div>";
	}
	return btnStr;
}

/**
 * 更改遮罩层的大小,确保在滚动以及窗口大小改变时还可以覆盖所有的内容
 * @return
 */
function re_mask(){
	var mask=document.getElementById("mask")	;
	if(null==mask)return;
	mask.style.width=get_width()+"px";
	mask.style.height=get_height()+"px";
}

/**
 * 更改弹出对话框层的位置,确保在滚动以及窗口大小改变时一直保持在网页的最中间
 * @return
 */
function re_pos(){
	var box=document.getElementById("msgbox");
	if(null!=box){
		var w=box.style.width;
		var h=box.style.height;
		h = parseInt(h.replace('px',''))+20;
		box.style.left=get_left(w)+"px";
		box.style.top=get_top(h)+"px";
	}
}

/**
 * 重新显示遮罩层以及弹出窗口元素
 * @return
 */
function re_show(){
	re_pos();
	re_mask();	
}

/**
 * 处理下拉框
 * @param visib
 * @return
 */
function displaySelectTag(visib){
	var selectTag = document.getElementsByTagName("select");
	for(var i=0; i<selectTag.length; i++){
		try{
			selectTag[i].style.visibility = visib;
		}catch(e){}
	}
}
/**
 * 清除遮罩层以及弹出的对话框
 * @return
 */
function removeDiv(){
	var mask=document.getElementById("mask");
	var msgbox=document.getElementById("msgbox");
	if(null != mask)
		document.body.removeChild(mask);
	if(null != msgbox)
		document.body.removeChild(msgbox);
	displaySelectTag("visible");
}

/**
 * 创建遮罩层的函数
 * @return
 */
function create_mask(){
	var mask=document.createElement("div");
	mask.id="mask";
	mask.style.position="absolute";
	mask.style.background="#777";
	mask.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75)";
	mask.style.opacity="0.6";//Mozilla的不透明设置
	mask.style.top="0px";
	mask.style.left="0px";
	mask.style.padding="0px";
	mask.style.width=get_width();
	mask.style.height=get_height();
	mask.style.zIndex=1000;
	
	// add iframe to solve the div is Covered by select tag
	var maskIframe = document.createElement("iframe");
	maskIframe.id="maskIframe";
	maskIframe.style.width="100%";
	maskIframe.style.height="100%";
	maskIframe.style.filter="Alpha(opacity=0)";
	maskIframe.border="0";
	maskIframe.frameborder="0";
	mask.appendChild(maskIframe);
	
	document.body.appendChild(mask);
	//window.scroll(0,0);
	var maskIframe=document.getElementById("maskIframe");
	if(null==maskIframe || maskIframe==undefined){
		//displaySelectTag("hidden");
	}
}

/**
 * 创建弹出对话框的函数
 * @param t: 内容
 * @param w: 宽度
 * @param h: 高度
 * @param btnCount: 按钮个数
 * @return
 */
function createMsgBox(t,w,h,btnCount){
	var msgbox=document.getElementById("msgbox");
	if(null != msgbox)
		document.body.removeChild(msgbox);
	var box=document.createElement("div");
	box.id="msgbox";
	box.style.cssText='width:'+w+'px;height:'+h+'px;border:2px solid #808080;background:#FFFFFF;position:absolute;padding:0px;z-index:1001;';
	var headTb='<table style="width:100%;height:18px;background:#183376;font:12px bolder;color:#fff; filter:progid:DXImageTransform.Microsoft.Alpha(startX=100, startY=100, finishX=20, finishY=20,style=1,opacity=75,finishOpacity=100);opacity:0.75;">';
		headTb += '<tr><td id="msgboxTitle">系统消息</td><td width="10px"><img src="img/window/winClose.png" onclick="javascript:removeDiv();"></td></tr></table>';
	var contHeight = btnCount==0?(h-24):(h-47);
	var bodyTb="<div style='width:99%;height:"+contHeight+"px;border:1px solid #000;margin:1px 0 0 1px;overflow:auto;'>"+t+"</div>";
	box.innerHTML = headTb+bodyTb+(0<btnCount?get_btn(btnCount):"");
	document.body.appendChild(box);
	re_pos();
}

/**
 * 创建弹出对话框的函数
 * @param t: 内容
 * @param w: 宽度
 * @param h: 高度
 * @param btnCount: 按钮个数
 * @return
 */
function createMsgBox2(t,w,h,btnCount){
	var msgbox=document.getElementById("msgbox");
	if(null != msgbox)
		document.body.removeChild(msgbox);
	var box=document.createElement("div");
	box.id="msgbox";
	box.style.cssText='width:'+w+'px;height:'+h+'px;border:1px solid #0080FF;background:#FFFFFF;position:absolute;padding:0px;z-index:1001';
	var headTb='<table style="width:100%;height:35px;background:#d0e4fd;font:14px bolder;color:#183376;">';
	headTb += '<tr><td id="calTitle" style="padding-left:32px;background:url(img/window/gg.gif) no-repeat 10px;">系统消息</td>';
	headTb += '<td width="18px"><img src="img/window/winClose.png" onclick="removeDiv()"></td>';
	headTb += '</tr></table>';
	var contHeight = btnCount==0?(h-40):(h-62);
	var bodyTb="<div style='width:100%;height:"+contHeight+"px;overflow:auto;'>"+t+"</div>";
	box.innerHTML = headTb+bodyTb+(0<btnCount?get_btn(btnCount):"");
	document.body.appendChild(box);
	re_pos();
}


// 点击确定
function btnOk(){
	if(beforeBtnOk())
		removeDiv();
	afterBtnOk();
}
// 点击确定前执行（需重写）
function beforeBtnOk(){
	return true;
}
// 点击确定后执行（需重写）
function afterBtnOk(){}

/*
 * 覆盖window的onresize和onscroll函数
*/
window.onresize=re_show;
window.onscroll=re_show;



/**********************toolTip Auth:qiyh**************************/
/*
 * 次方法需要用onclick调用
 * @param: w 宽度
 * @param： h 高度
 * @Auth: qiyh
 */
function showTip(str,w,h){
	var tipDiv = document.getElementById('tipDiv');
	if(null == tipDiv){
		tipDiv=document.createElement("div");
		tipDiv.id="tipDiv";
		tipDiv.style.position="absolute";
	}
	var evt = window.event ? window.event : null; 
	tipDiv.style.left=evt.x-30;//evt.clientX-30;
	tipDiv.style.top=evt.y+1;//evt.clientY+1;
	tipDiv.style.width=w;
	tipDiv.style.height=h;
	var innerStr = "<div style='position:absolute;top:14px;width:"+w+";height:"+(h-17)+";border:1px solid #686A3B;background:#FFFBE3;overflow:auto;padding:4px;word-break:break-all;'>"+str+"</div>";
	innerStr += "<div style='width:20px;height:15px;background:url(img/tip_top.png) no-repeat;position:absolute;left:20px;top:0;'></div>";
	innerStr += "<div style='width:12px;height:11px;position:absolute;top:4px;right:-4px;'><img src='img/clsTip.gif' onclick='hideTip()'></div>";
	tipDiv.innerHTML=innerStr;
	document.body.appendChild(tipDiv);
}
function hideTip(){
	var tipDiv = document.getElementById('tipDiv');
	if(null != tipDiv){
		document.body.removeChild(tipDiv);
	}
}


/*
 * 拖拽功能，可适用于任何div
 * divId：要拖拽移动的div
 * titleId: 对指定的title进行拖拽时才移动
 */
function Drag(divId,titleId) {
	titleId = titleId?titleId:divId;
    var $ = function (flag) {
        return document.getElementById(flag);
    }
    $(titleId).onmouseover = function(e){
    	this.style.cursor="move";
    }
    $(titleId).onmousedown = function(e){
        var d = document;
        var dv = $(divId);
        if (dv.setCapture) {
        	dv.setCapture();
        }
        else if (window.captureEvents){
        	window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
        }
        var page = {
            event: function (evt) {
                var ev = evt || window.event;
                return ev;
            },
            pageX: function (evt) {
                var e = this.event(evt);
                return e.pageX || (e.clientX + document.body.scrollLeft - document.body.clientLeft);
            },
            pageY: function (evt) {
                var e = this.event(evt);
                return e.pageY || (e.clientY + document.body.scrollTop - document.body.clientTop);
            },
            pageW: function(){
            	return document.documentElement.clientWidth-parseInt(dv.style.width.replace('px',''))-6;
            },
            pageH: function(){
            	return document.documentElement.clientHeight-parseInt(dv.style.height.replace('px',''));
            },
            layerX: function (evt) {
                var e = this.event(evt);
                return e.layerX || e.offsetX;
            },
            layerY: function (evt) {
                var e = this.event(evt);
                return e.layerY || e.offsetY;
            }
        }
        var x = page.layerX(e);
        var y = page.layerY(e);
        var bw = page.pageW();
    	var bh = page.pageH();
        d.onmousemove = function (e){                
            var tx = page.pageX(e) - x;
            if(tx>0 && tx<bw){
            	dv.style.left = tx + "px";
            }
            var ty = page.pageY(e) - y;
            if(ty>0 && ty<bh){
            	dv.style.top = ty + "px";
            }
        }
        d.onmouseup = function(){
            if (dv.releaseCapture) {
                dv.releaseCapture();
            }
            else if (window.releaseEvents) {
                window.releaseEvents(Event.MOUSEMOVE | Event.MOUSEUP);
            }
            d.onmousemove = null;
            d.onmouseup = null;
        }
    }
}

	
